var arrayLiFordepPackageFtp = [];
var arrayLiIndFordepPackageFtp = 0;
$(document).ready(function() {

	/* 依赖树 */
	$('#dependentPackage_selectionFtp').window({
		width:480,
		height:355,
		modal:true,
		minimizable:false,
		maximizable:false,
		collapsible:false,
		onClose: function () {
			$("#ttFordepPackageFtp").html("");
		}
	});
	$('#dependentPackage_selectionFtp').window('close');  //关闭窗口
	
	
	$("#seldepPackageBtnFtp").click(function(e) {
		//if(treeData==null){
			$("#ttFordepPackageFtp").tree({
				url:BASE_URL_HTTP_INTERFACE + '/getBundlesForEasyUiTree' + '?v=' + Math.random(),
				method: "get",
				lines:false,
				async:false,
				checkbox: true,
				loadFilter: function(data,parent) {
					/*treeData = [{text:"全部", children:data, nodeType:"root"}];
					return treeData;*/
					//alert(JSON.stringify(data));
					if(data.backValue == false){
						$.messager.alert('错误','<p>' + data.errorDes + '</p>', 'error');
					}else{
						treeData = [{text:"全部", children:data, nodeType:"root"}];
						return treeData;
					}
				},
				onLoadSuccess:function()
				{
					$("#ttFordepPackageFtp > li > ul > li > .tree-node > .tree-file").css("background","url('../../../images/st_folder.png') no-repeat 2px 2px");
					//$("#ttFordepPackageFtp > li > ul > li > .tree-node > .tree-file").parent().parent().css("display","none");
					reSetTreeFordepPackageFtp();
				},
				onClick:function(node){
					if(node.checked)
					{
						$("#ttFordepPackageFtp").tree("uncheck", node.target);
					}
					else
					{
						$("#ttFordepPackageFtp").tree("check", node.target);
					}
					
					//$("#systemName").val(node.text);
					//choose_tree();
				}
			});
		//}else{
			//reSetTree()
		//}
		
		
		return false;
	});
	
	$("#ensureSelectFordepPackageFtp").click(function (e) {
		var checkedNodes = $("#ttFordepPackageFtp").tree("getChecked");
		var selectSystems = "";
		var systemsName = "";
		for(var i = 0, len = checkedNodes.length; i < len; ++i)
		{
			var tempNode = checkedNodes[i];
			if(tempNode.nodeType != "bundle")
			{
				continue;
			}
			selectSystems += (selectSystems != ""? ",":"") + tempNode.id;
			systemsName += (systemsName != ""? ",":"") + tempNode.text;
		}
		$("#strdependsPackagesFtp").val(selectSystems);
		$("#dependsPackagesFtp").val(systemsName);
		$('#dependentPackage_selectionFtp').window('close');	
	});
	
	
	$("#searchNodeFordepPackageFtp").click(function (e) { 
		if($("#dependentPackageNameFtp").val() != '')
		{
			choose_treeFordepPackageFtp();
		}
		else
		{
			$(".dependentPackageNameFtp").val("");
			var row = $('#ttFordepPackageFtp li .tree-node');  
			row.removeClass('tree-node-selected');
			$(".tree_con").scrollTop(0);
		}
	});
	
	
	$("#dependentPackageNameFtp").bind('input propertychange', function() {
		arrayLiFordepPackageFtp = [];
		arrayLiIndFordepPackageFtp = 0;
		if($("#dependentPackageNameFtp").val() != '')
		{
			choose_treeFordepPackageFtp();
		}else
		{
			$("#ff input").val("");
			var row = $('#ttFordepPackageFtp li .tree-node');  
			row.removeClass('tree-node-selected');
			$(".tree_con").scrollTop(0);
		}
	});
	
	
	$(document , "#dependentPackageNameFtp").keyup(function(event){
		  if(event.keyCode == 8){
			$("#searchNodeFordepPackageFtp").trigger("click");
		  }
		});
	
	$(document , "#dependentPackageNameFtp").keyup(function(event){
		  if(event.keyCode ==13){
			 $("#searchNodeFordepPackageFtp").trigger("click");
		  }
		});
});



function setCheckedFordepPackageFtp(sysName) {
	if(!sysName)
	{
		return;
	}
	$("#ttFordepPackageFtp li .tree-node").each(function(){
		if($(".tree-title", $(this)).text() == sysName)
		{
			$('#ttFordepPackageFtp').tree('check', this);
			return false;
		}
	});
	
}



function setCheckedListFordepPackageFtp(sysNameArr)
{
	if(sysNameArr && sysNameArr.length)
	{
		for(var i = 0, len = sysNameArr.length; i < len; ++i)
		{
			setCheckedFordepPackageFtp(sysNameArr[i]);
		}
	}
}


function reSetTreeFordepPackageFtp(){
	$('#dependentPackage_selectionFtp').window('open');	
	var txtInterface = $("#dependsPackagesFtp").val().split(",")[0];

	if(txtInterface !== '')
	{
		$("#dependentPackageNameFtp").val(txtInterface);
		choose_treeFordepPackageFtp();
	}
	else
	{
		$("#dependentPackageNameFtp").val('');
		$(".tree_con").scrollTop(0);
		var row = $('#ttFordepPackageFtp li .tree-node');  
		row.removeClass('tree-node-selected');
	}
	
	var checkedNodes = $("#ttFordepPackageFtp").tree("getChecked");
	if(checkedNodes)
	{
		$.each(checkedNodes, function(index, node) {
			$("#ttFordepPackageFtp").tree("uncheck", node.target);
		});
	}
	
	var currSystemsStr = $("#dependsPackagesFtp").val();
	if(currSystemsStr != '')
	{
		setCheckedListFordepPackageFtp(currSystemsStr.split(","));
	}
}



function choose_treeFordepPackageFtp()
{
	$('#ttFordepPackageFtp').tree('expandAll');
	if(arrayLiFordepPackageFtp.length == 0 && arrayLiIndFordepPackageFtp == 0)
	{
		var searchVal = $("#dependentPackageNameFtp").val(); 
		var row = $('#ttFordepPackageFtp li .tree-node');  
		var rowcolumn = $('#ttFordepPackageFtp li .tree-node > .tree-title');  
		var i = 0, j = 0;
		var offsetC = 0
		row.removeClass('tree-node-selected');  
		rowcolumn.each(function (column) {  
			var matchVal = $(this).text(); 
			if(matchVal.indexOf(searchVal) != -1)
			{
				i++;
				var panel_top = parseInt($(this).parents(".panel").css("top"), 10);
				
				if(i <= 1)
				{
					$(this).parent().addClass('tree-node-selected'); 
					var top = $(".tree_con").scrollTop() + $(this).offset().top - 76 - panel_top;
					$(".tree_con").scrollTop(top);
					arrayLiIndFordepPackageFtp = 1;
				}			
				arrayLiFordepPackageFtp.push([matchVal,$(".tree_con").scrollTop() + $(this).offset().top - 76 - panel_top]);
				
			}
			}); 
		}else
		{
			var rowcolumn = $('#ttFordepPackageFtp li .tree-node > .tree-title'); 
			var row = $('#ttFordepPackageFtp li .tree-node');  
			row.removeClass('tree-node-selected');
			
			if(arrayLiIndFordepPackageFtp <  arrayLiFordepPackageFtp.length)
			{
				rowcolumn.each(function (column) {  
					var matchVal = $(this).text(); 
					if(matchVal == arrayLiFordepPackageFtp[arrayLiIndFordepPackageFtp][0])
					{
						$(this).parent().addClass('tree-node-selected'); 
						$(".tree_con").scrollTop(arrayLiFordepPackageFtp[arrayLiIndFordepPackageFtp][1]);
						arrayLiIndFordepPackageFtp++;
						return false;
					}
				});
			}else
			{
				//arrayLi = [];
				arrayLiIndFordepPackageFtp = 0;
				$(".tree_con").scrollTop(0);
				choose_treeFordepPackageFtp();
			}
		}
}